package com.yinhe.music.yhmusic.songmenu.presenter;

import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.IBaseModel;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.songmenu.SongMenuListActivity;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuListContract;

import io.reactivex.disposables.Disposable;

/**
 * {@link SongMenuListActivity} P层
 *
 * @author 何晓华
 */
public class SongMenuListPresenter extends BasePresenter<IBaseModel, ISongMenuListContract.ISongMenuListView> implements ISongMenuListContract.ISongMenuListPresenter {

    @Override
    public void getMenuList(int page, int pageSize, String type) {
        ISongMenuListContract.ISongMenuListView mView = getView();

        if (mView == null) {
            return;
        }

        Disposable collectDisposable = mModel.getSongMenuList(page, pageSize, type)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(songMenuList -> {
                    mView.setSongMenuListUI(songMenuList);
                }, throwable -> {
                    mView.setLoadMoreFailUI();
                    mView.showMessage(throwable);

                });

        mDisposable.add(collectDisposable);
    }

    @Override
    public void getSearchList(int page, int pageSize, String keyword) {
        if (getView() == null) {
            return;
        }

        if (page == 1) {
            getView().showLoading();
        }

        addSubscription(mModel.getSearchList(page, pageSize, keyword, IConstants.SONGMENU)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(searchInfo -> {
                            getView().hideLoading();
                            SongMenuList songMenuList = new SongMenuList(searchInfo.getPageNum(), searchInfo.getSongMenuList());
                            getView().setSongMenuListUI(songMenuList);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }

    @Override
    public void getUserSongMenu(String userId, int page, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getUserContent(userId, IConstants.SONGMENU, page, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(userContent -> {
                            SongMenuList songMenuList = new SongMenuList(userContent.getPageNum(), userContent.getSongMenuList());
                            getView().setSongMenuListUI(songMenuList);
                        },
                        throwable -> {
                            getView().hideLoading();
                            getView().showMessage(throwable);
                        }));
    }
}
